<template>
  <div class="home">
    <div
      style="
        margin: 10px 0 0 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
      "
    >
      <div>停车场列表</div>
      <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>停车场管理</el-breadcrumb-item>
          <el-breadcrumb-item>停车场信息</el-breadcrumb-item>
          <el-breadcrumb-item>停车场列表</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>

    <!-- 搜索区 重置区-->
    <div class="header">
      <el-input clearable v-model="parkingNum" placeholder="停车场编码"></el-input>
      <el-input clearable v-model="parkingName" placeholder="停车场名称"></el-input>
      <el-select clearable v-model="parkingType" placeholder="类型">
        <el-option label="地下停车库" value="0"></el-option>
        <el-option label="地面停车场" value="1"></el-option>
        <el-option label="地上停车楼" value="2"></el-option>
      </el-select>
      <el-button
        type="primary"
        class="el-icon-search"
        size="medium"
        @click="search"
        >查询</el-button
      >
      <el-button class="el-icon-refresh" size="medium" @click="reset"
        >重置</el-button
      >
    </div>

    <!-- 数据 -->
    <div style="width: auto; background: #fff">
      <div
        style="
          display: flex;
          justify-content: space-between;
          padding: 10px 0 10px 0;
        "
      >
        <p style="font-size: 15px; font-weight: bold; margin: 5px 0 0 20px">
          查询结果列表
        </p>
        <div style="margin: 0 30px 0 0">
          <el-button
            style="background: #0bb197"
            type="success"
            @click="addDialog = true"
            size="small"
            >新增</el-button
          >
        </div>
      </div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="序号" width="100" align="center">
        </el-table-column>
        <el-table-column prop="parkingNum" label="停车场编号" align="center">
        </el-table-column>
        <el-table-column prop="parkingName" label="停车场名称" align="center">
        </el-table-column>
        <el-table-column prop="parkingType" label="停车场类型" align="center">
          <template slot-scope="scope">
            <span>{{
              scope.row.parkingType == 0
                ? "地下停车场"
                : scope.row.parkingType == 1
                ? "地面停车场"
                : "地上停车楼"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="outletCount" label="出口个数" align="center">
        </el-table-column>
        <el-table-column prop="entranceCount" label="入口个数" align="center">
        </el-table-column>
        <el-table-column prop="floorTotal" label="总层数" align="center">
        </el-table-column>
        <el-table-column prop="spacesTotal" label="总车位数" align="center">
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              style="color: #0bb197"
              @click="edit(scope.row), (editDialog = true)"
              type="text"
              size="small"
              >编辑</el-button
            >
            <el-button
              style="color: #0bb197"
              @click="del(scope.row.id)"
              type="text"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="count"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :background="true"
        class="msg-pagination-container"
      >
      </el-pagination>
    </div>

    <!-- 新增的弹出框 -->
    <el-dialog title="新增区域" :visible.sync="addDialog">
      <el-form :model="addData">
        <!-- <el-form-item label="序号" :label-width="formLabelWidth">
          <el-input v-model="addData.id" autocomplete="off"></el-input>
        </el-form-item> -->
        <div class="addBox">
          <el-form-item style="width: 45%; margin-right: 10%" label="序号">
            <el-input v-model="addData.id" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            style="width: 45%"
            label="停车场编号"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="addData.parkingNum"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item
            style="width: 45%; margin-right: 10%"
            label="停车场名称"
          >
            <el-input
              v-model="addData.parkingName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="停车场类型" style="width: 45%">
            <el-select
              v-model="addData.parkingType"
              placeholder=""
              style="width: 100%"
            >
              <el-option label="地下停车场" value="0"></el-option>
              <el-option label="地面停车场" value="1"></el-option>
              <el-option label="地上停车楼" value="2"></el-option>
            </el-select>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item style="width: 45%; margin-right: 10%" label="出口个数">
            <el-input
              v-model="addData.outletCount"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item
            style="width: 45%"
            label="入口个数"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="addData.entranceCount"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item style="width: 45%; margin-right: 10%" label="总层数">
            <el-input
              v-model="addData.floorTotal"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item
            style="width: 45%"
            label="总车位数"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="addData.spacesTotal"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type=""
          style="margin-right: 30px"
          class="reset"
          @click="addDialog = false"
          >取 消</el-button
        >
        <el-button type="success" class="btnColor" @click="add"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑的弹出框 -->
    <el-dialog title="编辑区域" :visible.sync="editDialog">
      <el-form :model="editData">
        <!-- <el-form-item label="序号" :label-width="formLabelWidth">
          <el-input v-model="editData.id" autocomplete="off"></el-input>
        </el-form-item> -->
        <div class="addBox">
          <el-form-item label="序号" style="width: 45%; margin-right: 10%">
            <el-input
              disabled
              v-model="editData.id"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="停车场编号" style="width: 45%">
            <el-input v-model="editData.parkingNum" autocomplete="off">
            </el-input>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item
            style="width: 45%; margin-right: 10%"
            label="停车场名称"
          >
            <el-input
              v-model="editData.parkingName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="停车场类型" style="width: 45%">
            <el-select
              v-model="editData.parkingType"
              placeholder=""
              style="width: 100%"
            >
              <el-option label="地下停车场" value="0"></el-option>
              <el-option label="地面停车场" value="1"></el-option>
              <el-option label="地上停车楼" value="2"></el-option>
            </el-select>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item label="入口个数" style="width: 45%; margin-right: 10%">
            <el-input
              v-model="editData.outletCount"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="出口个数" style="width: 45%">
            <el-input
              disabled
              v-model="editData.entranceCount"
              autocomplete="off"
            >
            </el-input>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item label="总层数" style="width: 45%; margin-right: 10%">
            <el-input
              v-model="editData.floorTotal"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="总车位数" style="width: 45%">
            <el-input v-model="editData.spacesTotal" autocomplete="off">
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type=""
          style="margin-right: 30px"
          class="reset"
          @click="editDialog = false"
          >取 消</el-button
        >
        <el-button type="success" class="btnColor" @click="editFn"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      page: 1,
      count: 5,
      total: 1,
      parkingNum:'',
      parkingName:'',
      parkingType:'',
      addDialog: false,
      editDialog: false,
      formLabelWidth: "",
      id: "",
      user: "",
      currentPage4: 1,
      tableData: [],
      addData: {
        id: 0,
        parkingNum: "",
        parkingName: "",
        parkingType: "",
        outletCount: "",
        entranceCount:"",
        floorTotal:'',
        spacesTotal:'',
      },
      editData: {},
    };
  },
  methods: {
    //改变条数
    handleSizeChange(val) {
      this.count = val;
      this.getData();
      // console.log(`每页 ${val} 条`);
    },
    //翻页
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
      // console.log(`当前页: ${val}`);
    },
    //渲染
    getData: function () {
      this.$axios({
        url: "/smart/parking/list",
        method: "POST",
        params: {
          pageNum: this.page,
          pageSize: this.count,
        },
        data:{
          parkingNum:this.parkingNum,
          parkingName:this.parkingName,
          parkingType:this.parkingType,
        },
      }).then(({ data }) => {
        // console.log(data);
        if (data.code == '00000') {
          this.tableData = data.data.list;
          this.total = data.data.total;
        } else {
          this.$message.error("数据加载失败！");
        }
      });
    },
    //搜索
    search: function () {
      // this.$axios({
      //   url: "/smart/parking/queryById/" + this.id,
      //   method: "GET",
      // }).then(({ data }) => {
      //   console.log(data);
      //   if (data.code == 2000) {
      //     this.$message.success("查询成功");
      //     this.tableData = [data.this];
      //   } else {
      //     this.$message.error("查询失败");
      //   }
      // });
      this.getData();
    },
    //重置
    reset: function () {
      this.parkingNum = "";
      this.parkingName = "";
      this.parkingType = "";
      this.getData();
    },
    //新增
    add: function () {
      this.$axios({
        url: "/smart/parking/add",
        method: "POST",
        data:this.addData,
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.$message.success("添加成功");
          this.getData();
          this.addData={};
          this.addDialog = false;
        } else {
          console.log('失败',data)
          this.$message.error("添加失败");
        }
      });
    },
    //编辑
    edit: function (index) {
      let editData = { ...index };
      editData.parkingType=editData.parkingType+'';
      this.editData = editData;
      // console.log(this.editData);
    },
    editFn: function () {
      this.$axios({
        url: "/smart/parking/edit",
        method: "PUT",
        data: this.editData,
      }).then(({ data }) => {
        if (data.code == '00000') {
          console.log('成功',data)
          this.$message.success("修改成功");
          this.editDialog = false;
          this.getData();
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //删除
    del: function (index) {
      console.log(index);
      this.$axios({
        url: "/smart/parking/del/" + index,
        method: "DELETE",
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.$message.success("删除成功");
          this.getData();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
  },
  mounted: function () {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.home {
  height: 95%;
  margin: 20px 20px 0 20px;
}
.header {
  width: 100%;
  height: 80px;
  background: #fff;
  display: flex;
  align-items: center;
  margin: 30px 0 20px 0;
  .el-input,
  .el-select {
    width: 200px;
    margin: 10px;
  }
}
.el-icon-search {
  background: #0bb197;
  margin-left: 10px;
}
.el-icon-refresh {
  background-color: white;
  color: #c0c4cc;
  border: 1px solid #c0c4cc;
}
.el-icon-refresh:hover {
  color: #0bb197;
}
.el-pagination {
  margin-top: 30px;
  text-align: right;
}

//新增弹框样式
.el-dialog {
  width: 35%;
}
.btnColor {
  background-color: #0bb197;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
.addBox {
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}
.reset {
  background-color: white;
  color: #c0c4cc;
  border: 1px solid #c0c4cc;
}
.reset:hover {
  color: #0bb197;
}
.regionSet {
  .el-dialog {
    width: 820px;
  }
}
</style>
<style>
.msg-pagination-container.is-background .el-pager li:not(.disabled).active {
  /*当前选中页数的样式进行修改*/
  background-color: #0bb197;
  color: #fff;
}
</style>
